package bdhub.littletail.lib.utils;

import android.os.Environment;

import java.io.BufferedReader;
import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import bdhub.littletail.app.LittleTailApplication;

/**
 * Created by Freddy on 2015/11/20.
 * chenshichao@outlook.com
 */
public class FileUtils {

    /**
     * 检测SD卡是否存在
     */
    public static boolean checkSDcard() {
        return Environment.MEDIA_MOUNTED.equals(Environment
                .getExternalStorageState());
    }

    /**
     * 获取缓存目录文件夹
     *
     * @param folderName
     *            缓存目录下的文件夹名
     * @return 返回SD卡下的指定文件夹对象，若文件夹不存在则创建
     */
    public static File getCacheFolder(String folderName) {
        File file = null;
        if (checkSDcard()) {
            file = LittleTailApplication.sharedInstance().getExternalCacheDir();
        }
        if (null == file) {
            file = LittleTailApplication.sharedInstance().getCacheDir();
        }
        file = new File(file.getAbsoluteFile() + File.separator + folderName
                + File.separator);
        if (!file.exists()) {
            file.mkdirs();
        }
        return file;
    }

    /**
     * 输入流转字符串
     *
     * @param is
     * @return 一个流中的字符串
     */
    public static String inputStream2String(InputStream is) {
        if (null == is) {
            return null;
        }
        StringBuilder resultSb = null;
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            resultSb = new StringBuilder();
            String len;
            while (null != (len = br.readLine())) {
                resultSb.append(len);
            }
        } catch (Exception ex) {
        } finally {
            closeIO(is);
        }
        return null == resultSb ? null : resultSb.toString();
    }

    /**
     * 关闭流
     *
     * @param closeables
     */
    public static void closeIO(Closeable... closeables) {
        if (null == closeables || closeables.length <= 0) {
            return;
        }
        for (Closeable cb : closeables) {
            try {
                if (null == cb) {
                    continue;
                }
                cb.close();
            } catch (IOException e) {
                throw new RuntimeException(FileUtils.class.getClass().getName(), e);
            }
        }
    }

    /**
     * 写入文件到文本
     * @param filePath
     * @param data
     */
    public static void writeFile(String filePath, String data) {
        if (null == filePath) {
            return;
        }
        FileOutputStream fos;
        try {
            fos = new FileOutputStream(filePath);
            fos.write(data.getBytes());
            fos.flush();
            fos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 从文件中读取文本
     *
     * @param filePath
     * @return
     */
    public static String readFile(String filePath) {
        InputStream is = null;
        try {
            is = new FileInputStream(filePath);
        } catch (Exception e) {
            throw new RuntimeException(FileUtils.class.getName() + "readFile---->"
                    + filePath + " not found");
        }
        return inputStream2String(is);
    }
}
